﻿using Perspective.Wpf3D.Primitives;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;

namespace Perspective.Wpf3D.Sculptors
{
    /// <summary>
    /// 组成梯子的所有点和三角形集合
    /// A class to handle points and triangles of a 3D ladder model.
    /// Default radius of the head is 0.2.
    /// Default radius of the body is 0.1.
    /// By default, the direction of the ladder is the Z axis, and the length is 1.0.
    /// </summary>
    public class LadderSculptor : Sculptor
    {
        internal const double DefaultLength = 1.0;
        internal const double DefaultWidth = 0.5;
        internal const double DefaultStepHeight = 0.5;
        internal const double DefaultLongRadius = 0.05;
        internal const double DefaultShortRadius = 0.03;
        private double _length = DefaultLength;
        private double _width = DefaultWidth;
        private double _stepHeight = DefaultStepHeight;
        private double _longRadius = DefaultLongRadius;
        private double _shortRadius = DefaultShortRadius;
        /// <summary>
        /// Initializes a new instance of LadderSculptor.
        /// </summary>
        public LadderSculptor()
        {
        }

         /// <summary>
        /// Initializes a new instance of LadderSculptor.
        /// </summary>
        /// <param name="length">Length of each axis.</param>
        /// <param name="width">Width of each axis.</param>
        /// <param name="stepHeight">StepHeight of each axis.</param>
        /// <param name="longRadius">LongRadius of each axis.</param>
        /// <param name="shortRadius">ShortRadius of each axis.</param>
        public LadderSculptor(double length, double width, double stepHeight, double longRadius, double shortRadius)
        {
            Initialize(length,width,stepHeight, longRadius, shortRadius);
        }

        /// <summary>
        /// Initializes an existing instance of LadderSculptor.
        /// </summary>
        /// <param name="length">Length of each axis.</param>
        /// <param name="width">Width of each axis.</param>
        /// <param name="stepHeight">StepHeight of each axis.</param>
        /// <param name="longRadius">LongRadius of each axis.</param>
        /// <param name="shortRadius">ShortRadius of each axis.</param>
        public void Initialize(double length, double width, double stepHeight, double longRadius, double shortRadius)
        {
            _length = length;
            _width = width;
            _stepHeight = stepHeight;
            _longRadius = longRadius;
            _shortRadius = shortRadius;
        }
        /// <summary>
        /// Initializes the Points and Triangles collections.
        /// Called By Sculptor.BuildMesh()
        /// </summary>
        protected override void CreateTriangles()
        {
            PrismSculptor _barSculptorLeft = new PrismSculptor();
            _barSculptorLeft.Initialize(40, 0.0, 0.0);
            _barSculptorLeft.BuildMesh();
            Transform3DGroup tgLeft = new Transform3DGroup();
            AxisAngleRotation3D rXL = new AxisAngleRotation3D(
             new Vector3D(1.0, 0.0, 0.0),-90.0);
            tgLeft.Children.Add(new ScaleTransform3D(_longRadius * 2, _longRadius * 2, _length));
            tgLeft.Children.Add(new TranslateTransform3D(-_width/2, 0.0, 0));
            tgLeft.Children.Add(new RotateTransform3D(rXL, 0.0, 0.0, 0.0));
            _barSculptorLeft.Transform(tgLeft);
            CopyFrom(_barSculptorLeft);

            PrismSculptor _barSculptorRight = new PrismSculptor();
            _barSculptorRight.Initialize(40, 0.0, 0.0);
            _barSculptorRight.BuildMesh();
            Transform3DGroup tgRight = new Transform3DGroup();
            AxisAngleRotation3D rXR = new AxisAngleRotation3D(
             new Vector3D(1.0, 0.0, 0.0), -90.0);
            tgRight.Children.Add(new ScaleTransform3D(_longRadius * 2, _longRadius * 2, _length));
            tgRight.Children.Add(new TranslateTransform3D(_width / 2, 0.0, 0));
            tgRight.Children.Add(new RotateTransform3D(rXL, 0.0, 0.0, 0.0));
            _barSculptorRight.Transform(tgRight);
            CopyFrom(_barSculptorRight);

            int stepNumber = Convert.ToInt32(Math.Floor(_length / _stepHeight));
            for (int i = 0; i < stepNumber; i++)
            {
                PrismSculptor _barSculptorH = new PrismSculptor();
                _barSculptorH.Initialize(40, 0.0, 0.0);
                _barSculptorH.BuildMesh();
                Transform3DGroup tgH = new Transform3DGroup();
                AxisAngleRotation3D rYH = new AxisAngleRotation3D(
                 new Vector3D(.0, 1.0, 0.0), 90.0);
                tgH.Children.Add(new ScaleTransform3D(_shortRadius * 2, _shortRadius * 2, _width));
                tgH.Children.Add(new TranslateTransform3D(0, _stepHeight * i + _stepHeight/2, -_width / 2));
                tgH.Children.Add(new RotateTransform3D(rYH, 0.0, 0.0, 0.0));
                _barSculptorH.Transform(tgH);
                CopyFrom(_barSculptorH);
            }
        }

    }
}
